package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.DisciplineClassfiMapper;
import com.hbnu.system.model.entity.DisciplineClassfi;
import com.hbnu.system.service.DisciplineClassfiService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DisciplineClassfiServiceImpl extends ServiceImpl<DisciplineClassfiMapper, DisciplineClassfi> implements DisciplineClassfiService {
    @Override
    public boolean addDisciplineClassfi(DisciplineClassfi disciplineClassfi) {
        DisciplineClassfi projectcate1 = super.getOne(Wrappers.lambdaQuery(DisciplineClassfi.class)
                .eq(DisciplineClassfi::getDisciplineClassfiName,disciplineClassfi.getDisciplineClassfiName()));
        if(StrUtil.isEmpty(disciplineClassfi.getDisciplineClassfiName())||StrUtil.isBlank(disciplineClassfi.getDisciplineClassfiName())){
            throw new BaseException("该项目类别名称为空");
        }
        if(!Objects.isNull(projectcate1)){
            throw new BaseException("该学科分类名称已存在");
        }
        return baseMapper.insert(disciplineClassfi)==1;
    }

    @Override
    public List<DisciplineClassfi> getDisciplineClassfiList() {
        List<DisciplineClassfi> depts = baseMapper.selectList(Wrappers.lambdaQuery(DisciplineClassfi.class));
//        List<String> result = new ArrayList<>();
//        for (Dept dept : depts) {
//            result.add(dept.getDeptName());
//        }
        return depts;
    }

    @Override
    public boolean updateDisciplineClassfi(DisciplineClassfi disciplineClassfi) {
        if(StrUtil.isNotEmpty(disciplineClassfi.getDisciplineClassfiName())){
            DisciplineClassfi projectcate1 = super.getOne(Wrappers.lambdaQuery(DisciplineClassfi.class)
                    .eq(DisciplineClassfi::getDisciplineClassfiName,disciplineClassfi.getDisciplineClassfiName()));
            if(!Objects.isNull(projectcate1)){
                throw new BaseException("该学科分类名称已存在");
            }
            return baseMapper.updateById(disciplineClassfi)==1;
        }
        return false;
    }

    @Override
    public boolean deleteDisciplineClassfi(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<DisciplineClassfi> getByDisplineClassfiName(String disciplineClassfiName) {
        LambdaQueryWrapper<DisciplineClassfi> queryWrapper = Wrappers.lambdaQuery(DisciplineClassfi.class);

        // 如果 disciplineClassfiName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(disciplineClassfiName)) {
            queryWrapper.like(DisciplineClassfi::getDisciplineClassfiName, disciplineClassfiName);
        }

        return baseMapper.selectList(queryWrapper);
    }


}
